package com.yksj.healthtalk.ui.interestwall;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.math.NumberUtils;
import org.universalimageloader.core.DefaultConfigurationFactory;
import org.universalimageloader.core.DisplayImageOptions;
import org.universalimageloader.core.ImageLoader;
import org.universalimageloader.utils.StorageUtils;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.open.share.OpenManager;
import com.yksj.healthtalk.adapter.InterestAdapter;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.DoubleBtnFragmentDialog;
import com.yksj.healthtalk.comm.DoubleBtnFragmentDialog.OnDilaogClickListener;
import com.yksj.healthtalk.comm.ShareDialogFragment;
import com.yksj.healthtalk.comm.ZoomImgeDialogFragment;
import com.yksj.healthtalk.entity.CommendEntity;
import com.yksj.healthtalk.entity.GroupInfoEntity;
import com.yksj.healthtalk.entity.InterestWallImageEntity;
import com.yksj.healthtalk.entity.TagEntity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.ObjectHttpResponseHandler;
import com.yksj.healthtalk.net.socket.SmartControlClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.AppData;
import com.yksj.healthtalk.ui.chatting.ChatActivity;
import com.yksj.healthtalk.ui.salon.SalonSelectPaymentOptionActivity.OnBuyTicketHandlerListener;
import com.yksj.healthtalk.ui.views.TagsGridView;
import com.yksj.healthtalk.utils.FaceParse;
import com.yksj.healthtalk.utils.FileUtils;
import com.yksj.healthtalk.utils.JsonParseUtils;
import com.yksj.healthtalk.utils.PersonInfoUtil;
import com.yksj.healthtalk.utils.SalonHttpUtil;
import com.yksj.healthtalk.utils.ToastUtil;
import com.yksj.healthtalk.utils.WheelUtils;
import com.yksj.healthtalk.utils.XmlCategoryParserUtils;

/**
 * 兴趣墙点击图片进入图片详细 进入此界面intent需传递参数图片唯一id ( key:picid 类型为:String )
 * @author crj
 */
public class InterestContentActivity extends BaseFragmentActivity implements
		OnClickListener,OnBuyTicketHandlerListener{
	public final String TAG = "InterestContentActivity";
	public final String COLLECT_STORAGE = "interest_cllect";
	private PopupWindow sharePopWindow = null;//分享popupwindow
	private PopupWindow transmitPopupWindow = null;//转发popupwindow
	private int position = -1;//在tag为2的时候为0- 我发布  1-我收藏，其他情况该值 无用
	private GroupInfoEntity cacheGroupInfoEntity;
	private String picId;//图片id
	/**
	 * 表示我发布和收藏 还是从 所有兴趣墙图片界面点击进
	 * 1-从兴趣墙点击进入，2-从我发布我收藏点击进入
	 */
	private int tag;
	private ImageLoader mImageLoader;
	private DisplayImageOptions mDisplayImageOptions;
	
	//private DisplayImageOptions mHeaderDisplayImageOptions;
	
	private InterestWallImageEntity entity;
	private ImageView mBigImageView;//
	private TextView mTitleTextV;
	private TextView mContentTextV;
	private Button mShopButton;
	private ImageView mHeaderImageV;
	private TextView mTimeTextV;
	private TextView mCommentTextV;
	private TextView mUserTextV;
	private CheckBox mCollectCheckV;
	private Button mCommentButton;
	private Button mShareButton;
	private Button mRepeatButton;
	private String mUserId;
	private LinearLayout mContentLinearLayout;
	private LinearLayout mCommentLinearLayout;
	
	final BroadcastReceiver mReceiver = new BroadcastReceiver(){
		public void onReceive(Context context, Intent intent) {
			if (OpenManager.AUTH_RESULT_ACTION.equals(intent.getAction()))
			{
				Bundle bundle = intent.getExtras();
				int openType = bundle.getInt(OpenManager.BUNDLE_KEY_OPEN,0);
				shareImage(openType,getShareContent());
			}
		};
	};
	private TagsGridView mGridViewXingQu;
	private InterestAdapter mAdapterXingQu;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.interest_content);
		
		mImageLoader = ImageLoader.getInstance();
		mDisplayImageOptions = DefaultConfigurationFactory.createGalleryDisplayImageOptions(this);
		mUserId = SmartControlClient.getControlClient().getUserId();
		
		initView();
		initData(savedInstanceState);
	}
	
	private View onCreateHeaderView(){
		View view = getLayoutInflater().inflate(R.layout.interest_conent_list_header_layout, null);
		mBigImageView = (ImageView)view.findViewById(R.id.interestimage);
		mTitleTextV = (TextView)view.findViewById(R.id.interestdescribetitle);
		mContentTextV = (TextView)view.findViewById(R.id.interestdescribecontent);
		mShopButton = (Button)view.findViewById(R.id.interestshopbtn);
		mHeaderImageV = (ImageView)view.findViewById(R.id.interestheadimage);
		mUserTextV = (TextView)view.findViewById(R.id.interestusername);
		mTimeTextV = (TextView)view.findViewById(R.id.interesttime);
		mCommentTextV = (TextView)view.findViewById(R.id.interestcommendnum);
		mCollectCheckV = (CheckBox)view.findViewById(R.id.news_collection);
		mCommentButton = (Button)view.findViewById(R.id.news_comment);
		mShareButton = (Button)view.findViewById(R.id.news_share);
		mRepeatButton = (Button)view.findViewById(R.id.news_repeat);
		mBigImageView.setOnClickListener(this);
		mHeaderImageV.setOnClickListener(this);
		mCommentTextV.setOnClickListener(this);
		mCollectCheckV.setClickable(false);
			mCommentButton.setOnClickListener(this);
			mCollectCheckV.setOnClickListener(this);
			mShareButton.setOnClickListener(this);	
			mRepeatButton.setOnClickListener(this);
			if(!entity.getverifySuccess()){
			mCollectCheckV.setClickable(true);
		}
			mGridViewXingQu = (TagsGridView)view.findViewById(R.id.xingqu);
			List<TagEntity> list=new ArrayList<TagEntity>();
			mAdapterXingQu = new InterestAdapter(this, list,R.layout.textiterm);
			mGridViewXingQu.setAdapter(mAdapterXingQu);
		return view;
	}
	
	/**
	 * 没有获得数据前 对界面初始化，界面没有任何数据
	 */
	public void initView() {
		initTitle();
		mContentLinearLayout = (LinearLayout)findViewById(R.id.container);
		mCommentLinearLayout = (LinearLayout)findViewById(R.id.comment);
		
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		onQueryCommendForImage(picId);	
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		
		IntentFilter filter = new IntentFilter();
		filter.addAction(OpenManager.AUTH_RESULT_ACTION);
		registerReceiver(mReceiver,filter);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mReceiver);
		if(sharePopWindow!=null)sharePopWindow=null;
		if(transmitPopupWindow!=null)transmitPopupWindow=null;
	}
	
	/**
	 * 初始化参数
	 */
	public void initData(Bundle bundle) {
		if(bundle!=null){
			tag=bundle.getInt("tag", -1);
			position=bundle.getInt("position", -1);
			picId=bundle.getString("picId");
		}else{
			Intent intent = getIntent();
			tag = intent.getIntExtra("tag", -1);
			//图片id，通过此图片id获取整个这个界面的图片数据
			picId = intent.getStringExtra("picid");
			//如果是我发布和我收藏表示 图片所在的fragment是在0-我发布还是在1-我收藏内
			//如果是0 则增加一个删除按钮 如果是1 则增加一个把该改图片的收藏背景设置为已收藏
			position = intent.getIntExtra("position", -1);
		}
		
		if (picId != null) {
			//获取图片信息
			queryInterestImageInfo();
			//获取图片最近三条评论
			//getCommendForImage(String.valueOf(picid));
			//获取当前用户已经收藏列表，收藏列表内存储的都为picid
			//hasCollectList = utils.readObjectFromStorage(mUserId,this,COLLECT_STORAGE);
			//从我发布的 地方进入该界面 会有一个删除按钮
			if (tag == 2&&position==0) {
				titleRightBtn2.setBackgroundResource(R.drawable.icon_delete);
				titleRightBtn2.setVisibility(View.VISIBLE);
			} else {
				titleRightBtn2.setVisibility(View.INVISIBLE);
			}
		}
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("tag", tag);
		outState.putInt("position", position);
		outState.putString("picId", picId);
	}

	/**
	 * 获得数据后 初始化
	 * @param tag
	 */
	public void onParseData() {
		View view = onCreateHeaderView();
		mContentLinearLayout.removeAllViews();
		if(mContentLinearLayout.getChildCount() == 0){
			mContentLinearLayout.addView(view);
		}
		
		mImageLoader.displayImage(entity.getImagepath(),mBigImageView,mDisplayImageOptions);
		boolean isCollection = FileUtils.isInterestCollection(picId);
		mCollectCheckV.setChecked(isCollection);
		
		
		//图片根目录
		mImageLoader.displayImage(entity.getCUSTOMER_SEX(),entity.getCustomerHeadPic(),mHeaderImageV);
		mUserTextV.setText(entity.getCustomerNickname());
		mTimeTextV.setText(entity.getUploadTime());
		mCommentButton.setText(entity.getCommentCount());
		mShareButton.setText(entity.getShareCount());
		mRepeatButton.setText(entity.getTransmitCount());
		mTitleTextV.setText(entity.getPicName());
		mContentTextV.setText(entity.getPicDesc());
		mCollectCheckV.setText(entity.getCollectionCount());
		
		if(!TextUtils.isEmpty(entity.getPicUrl())){
			mShopButton.setVisibility(View.VISIBLE);
//			mShopButton.setText("进入店铺");
			mShopButton.setOnClickListener(this);
		}else if(!TextUtils.isEmpty(entity.getClassId())&&!TextUtils.isEmpty(entity.getShop_place())){
//			mShopButton.setText("进入他的摄影馆");
//			mShopButton.setOnClickListener(new View.OnClickListener() {
//				@Override
//				public void onClick(View v) {
//					Intent intent = new Intent(InterestContentActivity.this, MyPhotoGallery.class);
//					intent.putExtra("classId", entity.getClassId());
//					intent.putExtra("shopPlace", entity.getShop_place());
//					intent.putExtra("cusid", entity.getCustomerid());
//					startActivity(intent);
//				}
//			});
		}
		List<TagEntity> parToList = TagEntity.parToList(entity.getINFORMATION_LAY());
		if(parToList!=null && parToList.size()!=0)
			mAdapterXingQu.onBound(parToList);
		else{
			view.findViewById(R.id.interestting).setVisibility(View.GONE);
		}
		onQueryCommendForImage(picId);
	}

	
	public void initTitle() {
		super.initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleRightBtn2.setOnClickListener(this);
		titleTextV.setText("分享");
		titleTextV.setOnClickListener(this);
		titleLeftBtn.setOnClickListener(this);
	}
	
	
	/**
	 * 初始化最近三条评论界面包括评论内容
	 * @param data
	 */
	public void createCommendItem(Map<String, Object> data) {
		LayoutInflater inflater = getLayoutInflater();
		mCommentLinearLayout.removeAllViews();
		FaceParse parse = FaceParse.getChatFaceParse(this);
		for (String key : data.keySet()) {
			if("count".equals(key)){
				mCommentTextV.setText(data.get(key).toString()+"条评论");
				mCommentButton.setText(data.get(key).toString());
				continue;
			}
			final CommendEntity entity = (CommendEntity)data.get(key);
			View view = inflater.inflate(R.layout.interest_comment_item_layout,null);
			final ImageView imageView = (ImageView)view.findViewById(R.id.head_image);
			TextView nameTextV = (TextView)view.findViewById(R.id.nameTxt);
			TextView timeTextV = (TextView)view.findViewById(R.id.timeTxt);
			TextView contentTextV = (TextView)view.findViewById(R.id.contentTxt);
			nameTextV.setText(entity.getCustomerNickname());
			timeTextV.setText(entity.getSendTime());
			view.findViewById(R.id.chat_onclick).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					onSendJoinGoupChat(cacheGroupInfoEntity);
				}
			});
			contentTextV.setText(parse.parseSmileTxt(entity.getMessageContent()));
			mImageLoader.displayImage(entity.getSex(),entity.getIconUrl(),imageView);
			mCommentLinearLayout.addView(view);
			view.findViewById(R.id.read).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					InterestContentActivity.this.onClick(findViewById(R.id.news_comment));
				}
			});
		}
		View view = inflater.inflate(R.layout.load_more_layout,null);
		view.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				InterestContentActivity.this.onClick(findViewById(R.id.news_comment));
			}
		});
		
		mCommentLinearLayout.addView(view);
	}
	
	/**
	 * 
	 * 评论时候初始化评论实体，传入群聊界面
	 * @return
	 * 
	 */
	private GroupInfoEntity initCommendGroupEntity() {
		GroupInfoEntity entity1 = new GroupInfoEntity();
		entity1.setId(entity.getGroupId());
		entity1.setBigHeadIcon(entity.getImagepath());
		entity1.setCharge(false);
		entity1.setCreateCustomerID(entity.getCustomerid());
		entity1.setCreateTime(entity.getUploadTime());
		entity1.setCusMessag(entity.getRecordDesc());
		entity1.setGroupHeadPortraitID(entity.getGroupId());
		entity1.setGroupHeadPortraitName(entity.getRecordName());
		entity1.setGroupLevel(entity.getGroupLevel());
		entity1.setName(entity.getRecordName());
		entity1.setInceptMessage(true);
		//沙龙实体分类没有的时候为空，而兴趣墙为0
		if(entity.getInfoLayId().equals("0")) {
			entity1.setInfoId("");
			entity1.setInfoLayName("");
			entity1.setUpperId("");
		} else {
			//兴趣墙只有分类id，没有分类名称。所以要通过id把名称查找出来
			String[] ids = entity.getInfoLayId().split(",");
			//类名称以逗号分隔
			String names = "";
			//小类所在上级分类
			String upperId = "";
			if(ids.length>0) {
			for(int i=0;i<ids.length;i++) {
				TagEntity entity = XmlCategoryParserUtils.queryTagEntity(getResources(), R.xml.interestwall_class_store, ids[i]);
				names += entity.getName()+",";
				upperId += entity.getUpperId()+",";
			}
			entity1.setInfoId(entity.getInfoLayId());
			entity1.setInfoLayName(names.substring(0, names.lastIndexOf(",")));
			entity1.setUpperId(upperId.substring(0, upperId.lastIndexOf(",")));
			}
		}
		return entity1;
	}
	
	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2://删除图片
			
			DoubleBtnFragmentDialog.showDefault(getSupportFragmentManager(),  "是否删除图片？", "取消", "确定", new OnDilaogClickListener() {
				@Override
				public void onDismiss(DialogFragment fragment) {
				}
				@Override
				public void onClick(DialogFragment fragment, View v) {
					deleteImage();
				}
			});
			break;
		case R.id.news_collection://收藏
			//收藏按钮 点击第一次为收藏,第二次为取消收藏，所以需要增加一个标志位isCollectButtonClicked     
			//true为已经点击了收藏
			if(verifySuccess()){
			onUpdateCollectionNumber();}else{
				mCollectCheckV.setChecked(false);
			}
			break;
			
		case R.id.news_share:// 分享
			if(verifySuccess()){
			showSharePopWindow();}else{
				
			}
			break;
			
		case R.id.news_repeat:// 转发
			if(verifySuccess())
			showTransmitPopWindow();
			break;
			
		case R.id.cancel:
			if (sharePopWindow != null && sharePopWindow.isShowing())
				sharePopWindow.dismiss();
			if (transmitPopupWindow != null && transmitPopupWindow.isShowing())
				transmitPopupWindow.dismiss();
			break;
			
		case R.id.interestshopbtn:
			String path = entity.getPicUrl();
			Uri uri = Uri.parse(path.startsWith("http://")?path:"http://"+path);
			intent = new Intent(Intent.ACTION_VIEW,uri);
			startActivity(intent);
			break;
			
		case R.id.transmit_topic://转发沙龙
			transmitPopupWindow.dismiss();
			intent=new Intent(this,InterestwallForwardActivity.class);
			intent.putExtra("tag",2);
			intent.putExtra("picID", picId);
			startActivityForResult(intent, 1000);
			break;
			
		case R.id.transmit_friend://转发朋友
			transmitPopupWindow.dismiss();
			intent=new Intent(this,InterestwallForwardActivity.class);
			intent.putExtra("tag",1);
			intent.putExtra("picID", picId);
			startActivityForResult(intent, 2000);
			break;
			
		case R.id.sinashare:
			sharePopWindow.dismiss();
			shareImage(OpenManager.SINA_WEIBO,getShareContent());
			break;
			
		case R.id.messageshare:
			messageShare();
			break;
			
		case R.id.tencentshare:
			//腾讯微博需要每次分享都认证
			sharePopWindow.dismiss();
			shareImage(OpenManager.TENCENT_WEIBO,getShareContent());
			break;
		case R.id.interestimage://图片点击放大
			ZoomImgeDialogFragment.show(entity.getImagepath(),getSupportFragmentManager());
			break;
		case R.id.news_comment://评论
			if(verifySuccess())
			onSendJoinGoupChat(cacheGroupInfoEntity);
			break;
		case R.id.interestheadimage://查看个人资料
			
			PersonInfoUtil.choiceActivity(entity.getCustomerid(), this, entity.getIsDoctor());
//			String id = entity.getCustomerid();
//			if(id.equals(SmartFoxClient.getLoginUserId())){
//				intent = new Intent(InterestContentActivity.this,PersonalInformationActivity.class);
//			}else{
//				intent = new Intent(InterestContentActivity.this,FriendInformUI.class);
//				intent.putExtra("id",id);
//			}
//			startActivity(intent);
			break;
		}
	}
	
	private boolean verifySuccess(){
		if(!entity.getverifySuccess()){
			ToastUtil.showToastPanl("未审核通过,不能操作");
			return false;
		}else{
			return true;
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode!=Activity.RESULT_OK)return;
		switch (requestCode) {
		case 1000://转发给沙龙
		case 2000://转发给好友
			queryInterestImageInfo();
			//			if(data!=null&&data.hasExtra("sendState")){
//				if(1==data.getIntExtra("sendState",0)){
////					onActionSucceed("",0);
////					actionForImage(picId,mUserId,0);
//					queryInterestImageInfo();
//				}
//			}
			break;
		}}
	
	@Override
	protected void onResume() {
		super.onResume();
		queryInterestImageInfo();
	}
	
	/**
	 * 加入群聊
	 * @param entity
	 */
	private void onSendJoinGoupChat(GroupInfoEntity entity){
		SalonHttpUtil.onItemClick(this,this,getSupportFragmentManager(),entity,false);
	}
	
	
	
	/**
	 * 微博分享弹出PopupWindow
	 */
	public void showSharePopWindow() {
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.interest_share, null);
		View mainView = inflater.inflate(R.layout.interest_content, null);
		if (sharePopWindow == null) {
			sharePopWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
			sharePopWindow.setTouchable(true);
			sharePopWindow.setBackgroundDrawable(new BitmapDrawable());
			sharePopWindow.setOutsideTouchable(true);
			sharePopWindow.setAnimationStyle(R.style.AnimationPreview);
		}
		WheelUtils.setPopeWindow(this, mainView, sharePopWindow);
		Button sinashare = (Button) view.findViewById(R.id.sinashare);
		Button messageshare = (Button) view.findViewById(R.id.messageshare);
		view.findViewById(R.id.renrenshare).setVisibility(View.GONE);
		Button tencentshare = (Button) view.findViewById(R.id.tencentshare);
		Button cancel = (Button) view.findViewById(R.id.cancel);

		sinashare.setOnClickListener(this);
		messageshare.setOnClickListener(this);
		tencentshare.setOnClickListener(this);
		cancel.setOnClickListener(this);
	}

	/**
	 * 转发弹出窗
	 */
	public void showTransmitPopWindow() {
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.interest_transmit, null);
		View mainView = inflater.inflate(R.layout.interest_content, null);
		if (transmitPopupWindow == null) {
			transmitPopupWindow = new PopupWindow(view,LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			transmitPopupWindow.setTouchable(true);
			transmitPopupWindow.setBackgroundDrawable(new BitmapDrawable());
			transmitPopupWindow.setOutsideTouchable(true);
			transmitPopupWindow.setAnimationStyle(R.style.AnimationPreview);
		}
		WheelUtils.setPopeWindow(this, mainView, transmitPopupWindow);
		Button sinashare = (Button) view.findViewById(R.id.transmit_friend);
		Button messageshare = (Button) view.findViewById(R.id.transmit_topic);
		Button cancel = (Button) view.findViewById(R.id.cancel);

		sinashare.setOnClickListener(this);
		messageshare.setOnClickListener(this);
		cancel.setOnClickListener(this);
	}
	
	
	//获得分享图片路径
	public String getSharePicPath(){
		String name = StorageUtils.getFileName(entity.getImagepath());
		File file = new File(StorageUtils.getImagePath(),name);
		if(!file.exists())return null;
		return file.getAbsolutePath();
	}
	
	//获得分享内容
	public String getShareContent() {
//		return getResources().getString(R.string.sharepic, entity.getPicName(),entity.getPicDesc(),HttpRestClient.getmHttpUrls().URL_QUERYHEADIMAGE+entity.getImagepath());
		return  entity.getPicName()+","+entity.getPicDesc();
	}
	
	public String getMessageShareContent() {
		String txt =getShareContent();
		if (txt.length() > 65)
		txt = txt.substring(0, 65);
		txt = String.format(AppData.ShareContent, txt);
		return txt;
	}
	
	/**
	 * 短信分享
	 */
	public void messageShare() {
		Uri smsToUri = Uri.parse("smsto:");// 联系人地址 
        Intent mIntent = new Intent(android.content.Intent.ACTION_SENDTO, smsToUri);
    	
		mIntent.putExtra("sms_body", getMessageShareContent());
		startActivity(mIntent);
		if(sharePopWindow.isShowing())
		sharePopWindow.dismiss();
	}
	
	
	/**
	 * 分享成功时 更新服务器数据 分享数+1
	 */
	public void onShareSucceed() {
		actionForImage(picId,mUserId, 2);
	}
	
	
	/**
	 * 获取照片信息
	 */
	private void queryInterestImageInfo() {
		HttpRestClient.doHttpQueryInterestWallImage(picId,new ObjectHttpResponseHandler(this) {
					@Override
					public Object onParseResponse(String content) {
						return JsonParseUtils.parseInterestImageContentData(content);
					}
					@Override
					public void onSuccess(int statusCode, Object response) {
						entity = (InterestWallImageEntity) response;
						if (entity != null) {
							onParseData();
							cacheGroupInfoEntity = initCommendGroupEntity();
						} 
					}
				});
	}

	private void onUpdateCollectionNumber(){
		boolean isCollect = mCollectCheckV.isChecked();
		if(isCollect) {
			actionForImage(picId,mUserId,1);
		} else {
			onDeleteCollection();
		}
		FileUtils.updateInterestCollection(picId,isCollect);
	}
	
	/**
	 * 收藏分享转发
	 * @param picId
	 * @param userid
	 * @param type
	 */
	private void actionForImage(final String picId, String userid, final int type) {
		HttpRestClient.doHttpActionForImage(picId, userid, String.valueOf(type),
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(int statusCode, String content) {
						super.onSuccess(statusCode, content);
						onActionSucceed(content,type);
					}
				});
	}
	
	/**
	 * 通过图片id 查看图片最近三条评论
	 * @param picid
	 */
	private void onQueryCommendForImage(String picid) {
		HttpRestClient.doHttpQueryInterestImageCommend(picid,
				new ObjectHttpResponseHandler() {
					@Override
					public Object onParseResponse(String cotent) {
						return JsonParseUtils.onCommendJsonParse(cotent);
					}
					@Override
					public void onSuccess(int statusCode, Object response) {
						createCommendItem((Map<String, Object>)response);
					}
				});
	}
	
	private void onShowToast(String conent){
		ToastUtil.showLong(getApplicationContext(),conent);
	}

	/**
	 * 删除图片
	 */
	private void deleteImage() {
		if(entity == null)return;
		HttpRestClient.doHttpDeleteInterestImage(SmartFoxClient.getLoginUserId(),picId,new AsyncHttpResponseHandler(this) {
			@Override
			public void onSuccess(int statusCode, String response) {
				super.onSuccess(statusCode, response);
					onImageDeleteSucceed(response);
			}
		});
	}
	
	/**
	 * 取消收藏
	 */
	private void onDeleteCollection() {
		HttpRestClient.doHttpCancelCollect(mUserId,picId,new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode, String response) {
				super.onSuccess(statusCode, response);
				String number = mCollectCheckV.getText().toString();
				if(NumberUtils.isNumber(number)){
					int collectNumber = NumberUtils.toInt(number);
					--collectNumber;
					if(collectNumber < 0)collectNumber = 0;
					mCollectCheckV.setText(String.valueOf(collectNumber));
				mCollectCheckV.setChecked(false);
			}}
		});
	}
	
	/**
	 * 网络请求后数据处理-----------------------------------------------------------------
	 */
	private void onImageDeleteSucceed(Object object) {
		if("Y".equals(object.toString().trim())) {
			onShowToast("删除成功！");
			Intent intent1 = new Intent();
			intent1.putExtra("picid",picId);
			setResult(RESULT_OK, intent1);
			finish();
		} else {
			onShowToast("删除失败！");
		}
	}
	
	/**
	 * 收藏分享转发成功后返回内容
	 * @param object
	 * @param type
	 */
	private void onActionSucceed(String object, int type) {
		if(1 == type) {//1 为收藏状态
			String number = mCollectCheckV.getText().toString();
			if(NumberUtils.isNumber(number)){
				int collectNumber = NumberUtils.toInt(number);
				++collectNumber;
				if(collectNumber < 0)collectNumber = 0;
				mCollectCheckV.setText(String.valueOf(collectNumber));
				mCollectCheckV.setChecked(true);
			}
		} else if(2 == type){//2为分享状态
			String number = mShareButton.getText().toString();
			if(NumberUtils.isNumber(number)){
				int collectNumber = NumberUtils.toInt(number);
				++collectNumber;
				if(collectNumber < 0)collectNumber = 0;
				mShareButton.setText(String.valueOf(collectNumber));
			}
		}else if(0==type){//0为转发状态
			String number = mRepeatButton.getText().toString();
			if(NumberUtils.isNumber(number)){
				int collectNumber = NumberUtils.toInt(number);
				++collectNumber;
				if(collectNumber < 0)collectNumber = 0;
				mRepeatButton.setText(String.valueOf(collectNumber));
			}
		}
	}
	
	
	/**
	 * 分享图片+文字
	 * @param type
	 * @param path
	 * @param txt
	 */
	public void shareImage(int type,String txt) {
		if (StorageUtils.isSDMounted()) {
			String dir = StorageUtils.getImagePath();
			String imagePath = entity.getImagepath();
			File file = mImageLoader.getOnDiscFileName(new File(dir),imagePath);
			if (file.exists()) {
//				Bundle bundle = new Bundle();
				if (txt.length() > 65)
					txt = txt.substring(0, 65);
				txt = String.format(AppData.ShareContent, txt);
//				bundle.putString(OpenManager.BUNDLE_KEY_IMGPATH,file.getAbsolutePath());
//				bundle.putString(OpenManager.BUNDLE_KEY_TEXT, txt);
				
				OpenManager openManager = OpenManager.getInstatnce();
				boolean isEnable = OpenManager.getInstatnce().isVaild(type,this);
				if(!isEnable)openManager.authorize(type,this);else{
				
				ShareDialogFragment.show(getSupportFragmentManager(), type,txt, file.getAbsolutePath(),
						new ShareDialogFragment.OnShareResultListener() {
							@Override
							public void onResult(int result) {
								if (result == 0) {
									onShareSucceed();
								}
							}
						});
				}
			} else {
				ToastUtil.showShort(getApplicationContext(), "图片不存在...");
			}
		} else {
			ToastUtil.showShort(getApplicationContext(), "对不起,手机内存不足,请及时处理");
		}
	}
	
	
	@Override
	public void onTicketHandler(String state, GroupInfoEntity entity) {
		if("1".equals(state)){
			Intent  intent1 = new Intent(this,ChatActivity.class);
			intent1.putExtra(ChatActivity.KEY_PARAME,entity);
			startActivity(intent1);
		}else if("0".equals(state)){
			ToastUtil.showLong(this,"金额不足");
		}else if("-1".equals(state)){
			ToastUtil.showLong(this,"支付失败");
		}
	}
}
